package game.module.item.dao;

import game.constants.GameConstant;
import game.constants.Position;
import game.module.item.beans.CharacterGoods;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

@Component
public class GoodsCache {

	@Autowired
	public CacheManager cacheManager;

	@Autowired
	private GoodsDao goodsDao;

	public void load(int heroId) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS);
		List<CharacterGoods> goodsList = null;
		Map<Short, CharacterGoods> bodyGoodMap = new ConcurrentHashMap<Short, CharacterGoods>();
		Map<Short, CharacterGoods> bagGoodMap = new ConcurrentHashMap<Short, CharacterGoods>();
		cache.put(GameConstant.CACHE_GOODS_BAG + heroId, bagGoodMap);
		cache.put(GameConstant.CACHE_GOODS_BODY + heroId, bodyGoodMap);
		try {
			goodsList = goodsDao.getCharacterGoodsList(heroId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		for (CharacterGoods characterGoods : goodsList) {
			short position = characterGoods.getPosition();
			if ((position >= Position.BodyGoodsBeginPostion && position <= Position.BodyGoodsEndPostion)) {
				bodyGoodMap.put(position, characterGoods);
			} else if ((position >= Position.HorseGoodsBeginPostion && position <= Position.HorseGoodsEndPostion)) {
			} else if (position >= Position.BagGoodsBeginPostion && position <= Position.BagGoodsEndPostion) {
				bagGoodMap.put(position, characterGoods);
			} else if (position >= Position.StorageGoodsBeginPostion && position <= Position.StorageGoodsEndPostion) {
			} else if (position >= Position.StallGoodsBeginPostion && position <= Position.StallGoodsEndPostion) {
			}
		}
	}

	@SuppressWarnings("unchecked")
	public Map<Short, CharacterGoods> getBodyGoodsMap(int heroId) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS);
		return (Map<Short, CharacterGoods>) cache.get(GameConstant.CACHE_GOODS_BODY + heroId);
	}

	@SuppressWarnings("unchecked")
	public Map<Short, CharacterGoods> getBagGoodsMap(int heroId) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS);
		return (Map<Short, CharacterGoods>) cache.get(GameConstant.CACHE_GOODS_BAG + heroId);
	}

}
